<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>陈亮2021-07-13 10:37-</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #mian div {
            width: 100vw;
            height: 100vh;
            font-size: 100px;
            color: white;
        }

        #mian .header,
        #mian .footer {
            height: 150vh;
        }

        .header {
            background-color: darkorange;
        }

        .one {
            background-color: rebeccapurple;
        }

        .two {
            background-color: sienna;
        }

        .three {
            background-color: royalblue;
        }

        .four {
            background-color: palegreen;
        }

        .footer {
            background-color: palevioletred;
        }

        .nav {
            position: fixed;
            width: 50px;
            height: 200px;
            border: 1px solid #ccc;
            right: 20px;
            top: 200px;
            background-color: white;
        }

        .nav div {
            height: 50px;
            text-align: center;
            line-height: 50px;
            cursor: pointer;

        }

        .nav div.active {
            background-color: tomato;
        }
    </style>
</head>

<body>
    <div id="mian">
        <div class="header">header</div>
        <div class="one">one</div>
        <div class="two">two</div>
        <div class="three">three</div>
        <div class="four">four</div>
        <div class="footer">footer</div>
    </div>
    <div class="nav">
        <div class="first  active">one</div>
        <div class="second">two</div>
        <div class="third">three</div>
        <div class="fourth">four</div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <script>
        let oneTop = $(`.one`).offset().top
        let twoTop = $(`.two`).offset().top
        let threeTop = $(`.three`).offset().top
        let fourTop = $(`.four`).offset().top
        let footerTop = $(`.footer`).offset().top
        $(window).scroll(function () {
            let scrollTop = $(window).scrollTop()
            if (scrollTop >= footerTop-1) {
                $(`.nav div`).removeClass(`active`)
            } else if (scrollTop >= fourTop-50) {
                $(`.fourth`).addClass(`active`).siblings(`.active`).removeClass(`active`)
            } else if (scrollTop >= threeTop-50) {
                $(`.third`).addClass(`active`).siblings(`.active`).removeClass(`active`)
            } else if (scrollTop >= twoTop-50) {
                $(`.second`).addClass(`active`).siblings(`.active`).removeClass(`active`)
            } else if (scrollTop >= oneTop-50)  {
                $(`.first`).addClass(`active`).siblings(`.active`).removeClass(`active`)
            } else {
               $(`.nav div`).removeClass(`active`)
            }
        })
        $(`.nav div`).click(function(){
            let index=$(this).index()
let scrollTop=$($(`#mian div:not(:first,:last)`)[index]).offset().top
$(window).scrollTop(scrollTop)
        })

    </script>
</body>

</html>